using System;
using System.Collections.Generic;
using System.Text;

namespace SysComm
{
    public class CMFEncode
    {
        #region Constant
        const int MAX_BUFFER = 256;
        private char[] ENCODE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        #endregion

        #region Variables
        private bool m_bEncode = false;
        private int[] m_nEncode;
        private int[] m_nEncode1;
        #endregion

        #region Constructor
        public CMFEncode(bool bEncode, string strKey)
        {
            m_bEncode = bEncode;
            init(strKey);
        }
        #endregion

        #region Init
        private bool init(string strKey)
        {
            strKey = convertEncodingKey(strKey);

            if (strKey != null && strKey.Length < MAX_BUFFER)
            {
                byte[] bytTemp = Encoding.UTF8.GetBytes(strKey);
                int nLength = bytTemp.Length;
                int nTemp = 0;
                int nTemp1 = 0;

                m_nEncode = new int[MAX_BUFFER];
                m_nEncode1 = new int[MAX_BUFFER];

                for (int i = 0; i < MAX_BUFFER; ++i)
                {
                    m_nEncode[i] = i;
                }

                for (int i = 0; i < MAX_BUFFER; ++i)
                {
                    int nEncode = m_nEncode[i];
                    nTemp1 = (bytTemp[nTemp] & 0xFF) + nEncode + nTemp1 & 0xFF;
                    m_nEncode[i] = m_nEncode[nTemp1];
                    m_nEncode[nTemp1] = nEncode;
                    nTemp = (nTemp + 1) % nLength;
                }

                for (int i = 0; i < MAX_BUFFER; ++i)
                {
                    m_nEncode1[i] = m_nEncode[i];
                }

                return true;
            }

            return false;
        }

        private string convertEncodingKey(string strKey)
        {
            string str = new string(new char[] { 'v', '8', 'm', '5', 'j', 'h', '0', '2', 'f', 'l', 'e', '7', 'y', 'b', 'x', '2', 'q', 't', 'f', '9' });

            return str + strKey;
        }
        #endregion

        #region Encode
        public string encode(string strValue)
        {
            if (strValue != null && m_nEncode != null)
            {
                byte[] byteTemp;

                if (m_bEncode)
                {
                    byteTemp = Encoding.UTF8.GetBytes(strValue);
                }
                else
                {
                    byteTemp = Encoding.UTF8.GetBytes(strValue);
                }

                return encode(byteTemp, true);
            }

            return null;
        }

        private string encode(byte[] byteValue)
        {
            return Encoding.UTF8.GetString(byteValue);
        }

        private string encode(byte[] byteValue, bool bDoubleByte)
        {
            int nLength = byteValue.Length;
            int nTemp = 0;
            int nTemp1 = 0;
            string strValue = "";
            byte[] byteTemp = new byte[nLength];

            for (int i = 0; i < MAX_BUFFER; ++i)
            {
                m_nEncode[i] = m_nEncode1[i];
            }

            for (int i = 0; i < nLength; ++i)
            {
                nTemp = nTemp + 1 & 0xFF;
                nTemp1 = m_nEncode[nTemp] + nTemp1 & 0xFF;
                int nEncode = m_nEncode[nTemp];
                m_nEncode[nTemp] = m_nEncode[nTemp1];
                m_nEncode[nTemp1] = nEncode;
                nEncode = m_nEncode[nTemp] + m_nEncode[nTemp1] & 0xFF;
                byteTemp[i] = (byte)(byteValue[i] ^ m_nEncode[nEncode]);
            }

            if (bDoubleByte)
            {
                for (int i = 0; i < byteTemp.Length; ++i)
                {
                    byte bytMFValue = byteTemp[i];
                    strValue += ENCODE[((bytMFValue & 0xF0) >> 4)];
                    strValue += ENCODE[(bytMFValue & 0xF)];
                }
            }
            else if (m_bEncode)
            {
                strValue = encode(byteTemp);
            }
            else
            {
                strValue = Encoding.UTF8.GetString(byteTemp);
            }

            return strValue;
        }
        #endregion
    }
}
